<template>
  <div>
    <div class="detail">
      <div class="header">
        <van-icon
          name="arrow-left"
          @click="back"
          size="8rem"
          color="#ffcd32"
          class="goBackBtn"
        />
        <div class="singerName">{{ name }}</div>
      </div>
      <img :src="url" alt="" />
      <div v-if="loadingFlag" class="loading">
        <van-loading color="#ffcd32" text-color="#ffcd32" text-size="12px">
          请稍等,正在加载中.....
        </van-loading>
      </div>
      <!-- 歌手列表 -->
      <ul v-else>
        <li v-for="(item, index) in list" :key="item.id" @click="play(index)">
          <h2>{{ item.name }}</h2>
          <p>{{ item?.album || "咱也不知道啥专辑" }}</p>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { getSongList, getSongsUrl } from "@axios/service";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      loadingFlag: true,
      isLoading: true,
      name: "暂无数据",
      url: "",
      list: [],
      urls: [],
    };
  },
  methods: {
    ...mapMutations(["changeSongList", "changeCurrIndex", "changeFullScreen"]),
    play(index) {
      if (this.urls && this.urls.length > 0) {
        // 将歌曲url列表 和  信息列表进行合并
        const arr = this.list.map((item, index) => {
          return { ...item, url: this.urls[index] };
        });
        this.changeSongList(arr);
        this.changeCurrIndex(index);
        this.changeFullScreen(true);
      }
    },
    back() {
      this.$router.go(-1);
    },
    async getSongsUrlData(mids) {
      const { err, urls } = (await getSongsUrl(mids)) || {};
      if (err) return false;
      this.urls = urls || [];
    },
    async getSongData(singermid) {
      const { code, result } = (await getSongList(singermid)) || {};
      this.loadingFlag = false;
      if (code) return false;
      // 根据mid 获取歌曲的url
      let mids = result?.songs.map((item) => item.mid);
      this.getSongsUrlData(mids);
      this.list = result?.songs || [];
    },
    handImg(mid) {
      if (!mid)
        return "https://img1.baidu.com/it/u=4112794715,1317791462&fm=253&fmt=auto&app=138&f=JPEG?w=690&h=340";
      return `http://y.gtimg.cn/music/photo_new/T001R800x800M000${mid}.jpg`;
    },
  },
  created() {
    this.name = this.$route.query.name;
    this.url = this.handImg(this.$route.params.id);
    this.getSongData(this.$route.params.id);
  },
};
</script>
<style lang="less" scoped>
@import "@styles/index.less";
.loading {
  position: fixed;
  .top(350);
  left: 0;
  right: 0;
  bottom: 0;
  background: @bgcolor;
  z-index: 3;
  text-align: center;
  .lh(200);
}
.detail {
  overflow: scroll;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: @bgcolor;
  z-index: 3;
  .header {
    position: fixed;
    top: 0;
    .wh(375, 40);
    background: #07111b66;
    .goBackBtn {
      position: absolute;
      top: 5px;
      left: 5px;
    }
    .singerName {
      text-align: center;
      .fs(20);
      color: #fff;
      top: 0;
      .lh(40);
    }
  }
  img {
    .w(375);
    .h(340);
  }
  ul {
    .padding(20,30,30,20);
    li {
      .wh(315,75);
      display: flex;
      box-sizing: border-box;
      flex-direction: column;
      h2 {
        .fs(15);
        font-weight: bolder;
      }
      p {
        .fs(14);
        color: rgba(225, 225, 225, 0.6);
        .margin(10,0,0,0);
      }
    }
  }
}
.detail-loading {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 3;
  background: @bgcolor;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
